﻿using Avalonia.Media.Imaging;

namespace KinonekoSoftware.Drawings
{
    public static partial class Snapshot
    {
        public static RenderTargetBitmap Capture(Control target, int ratio = 100)
        {
            if (target is null)
            {
                return null;
            }

            var w = target.Bounds.Width;
            var h = target.Bounds.Height;
            // var dpi  = target.GetDpi(target);
            var dpiX = 96d * ratio / 100; //* dpi.DpiScaleX;
            var dpiY = 96d * ratio / 100; //* dpi.DpiScaleY;

            //
            // 暂时不知道Avalonia需不需要Border参与
            // if (target is not Border root)
            // {
            //     //
            //     // Windows 平台截图工作需要有Border参与,所以需要创建一个Dummy Border
            //     root = new Border
            //     {
            //         Background = new VisualBrush
            //         {
            //             Visual = target
            //         },
            //         Width  = w,
            //         Height = h
            //     };
            //
            //     target.Measure(new Size(w, h));
            //     target.Arrange(new Rect(0, 0, w, h));
            // }


            //
            // 创建RenderTargetBitmap
            var pixelSize = new PixelSize((int)(w * dpiX / 96), (int)(h * dpiY / 96));
            var dpiVec    = new Vector(dpiX, dpiY);
            var bitmap    = new RenderTargetBitmap(pixelSize, dpiVec);


            bitmap.Render(target);
            return bitmap;
        }
        
        public static RenderTargetBitmap Capture(FrameworkElement target, int ratio = 100)
        {
            if (target is null)
            {
                return null;
            }

            var w = target.Bounds.Width;
            var h = target.Bounds.Height;
            // var dpi  = target.GetDpi(target);
            var dpiX = 96d * ratio / 100; //* dpi.DpiScaleX;
            var dpiY = 96d * ratio / 100; //* dpi.DpiScaleY;

            //
            // 暂时不知道Avalonia需不需要Border参与
            // if (target is not Border root)
            // {
            //     //
            //     // Windows 平台截图工作需要有Border参与,所以需要创建一个Dummy Border
            //     root = new Border
            //     {
            //         Background = new VisualBrush
            //         {
            //             Visual = target
            //         },
            //         Width  = w,
            //         Height = h
            //     };
            //
            //     target.Measure(new Size(w, h));
            //     target.Arrange(new Rect(0, 0, w, h));
            // }


            //
            // 创建RenderTargetBitmap
            var pixelSize = new PixelSize((int)(w * dpiX / 96), (int)(h * dpiY / 96));
            var dpiVec    = new Vector(dpiX, dpiY);
            var bitmap    = new RenderTargetBitmap(pixelSize, dpiVec);


            bitmap.Render(target);
            return bitmap;
        }
        
        public static MemoryStream CaptureToStream(Control target, int ratio = 100)
        {
            if (target is null)
            {
                return null;
            }

            var bitmap = Capture(target, ratio);
            var ms     = new MemoryStream();
            bitmap.Save(ms);

            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
        
        
        public static MemoryStream CaptureToStream(FrameworkElement target, int ratio = 100)
        {
            if (target is null)
            {
                return null;
            }

            var bitmap = Capture(target, ratio);
            var ms     = new MemoryStream();
            bitmap.Save(ms);

            ms.Seek(0, SeekOrigin.Begin);
            return ms;
        }
    }
}